/*
 PureMVC AS3 Demo - Flex CafeTownsend
 Copyright (c) 2007-08 Michael Ramirez <michael.ramirez@puremvc.org>
 Parts Copyright (c) 2005-07 Adobe Systems, Inc. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
package vn.demand.dragon.view
{
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.SupplierProxy;
	import vn.demand.dragon.model.vo.Supplier;
	import vn.demand.dragon.view.components.SuppliersList;
    
    /**
     * A Mediator for interacting with the EmployeeList component
     */
    public class SupplierListMediator extends Mediator implements IMediator
    {
        // Cannonical name of the Mediator
        public static const NAME:String = "SuppliersListMediator";
        
        public var id:String = null;
        
        /**
         * Constructor. 
         */
        public function SupplierListMediator(viewComponent:Object ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( NAME, viewComponent );
			
			supplierProxy = SupplierProxy( facade.retrieveProxy( SupplierProxy.NAME ) );
			masterdataProxy = MasterdataProxy( facade.retrieveProxy( MasterdataProxy.NAME ) );
			
			supplierList.addEventListener(SuppliersList.SELECT_SUPPLIER, selectSupplier);
			supplierList.addEventListener(SuppliersList.NEW_SUPPLIER, newSupplier);
			supplierList.addEventListener(SuppliersList.REMOVE_SUPPLIER, removeSupplier);
			supplierList.addEventListener(SuppliersList.CLOSE_SUPPLIERS_LIST, closeSuppliersList);
			supplierList.addEventListener(SuppliersList.REFRESH_SUPPLIERS_LIST, onRefreshSuppliersList);
        }

		public function newSupplier(event:Event):void {
			sendNotification(CommonCommand.ADD_SUPPLIER);
		}
		
		public function removeSupplier(event:Event):void {
			var sel:Supplier = new Supplier(supplierList.dgSupplier.selectedItem);
			alert.confirm("Do you want to remove [" + sel.sup_name + "]?", function(e:CloseEvent) {
    		if (e.detail == Alert.NO) {
        			return;
        		}
        		
        		sendNotification(CommonCommand.REMOVE_SUPPLIER, sel);
        	});
		}
		
		public function onRefreshSuppliersList(event:Event):void {
			doRefreshSuppliersList();
		}
		
		public function doRefreshSuppliersList():void {
			supplierProxy.search();
		}

		public function selectSupplier(event:Event):void {
			if (supplierList.dgSupplier.selectedIndex < 0) {
				alert.error("You should select a supplier");
				return;
			}
			var supplier = new Supplier(supplierList.dgSupplier.selectedItem);
			sendNotification(CommonCommand.EDIT_SUPPLIER, supplier);
		}
		
		public function closeSuppliersList(event:Event):void {
			sendNotification(CommonCommand.CLOSE_SUPPLIERS_LIST, supplierList);
		}

        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            return [ CommonCommand.LOAD_SUPPLIERS_SUCCESSFUL,            
					CommonCommand.LOAD_SUPPLIERS_FAILED,
					CommonCommand.SUPPLIER_UPDATED,
					CommonCommand.SUPPLIER_ADDED,
					CommonCommand.SUPPLIER_REMOVE_FAILED,
					CommonCommand.SUPPLIER_REMOVED,
			];
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
                case CommonCommand.LOAD_SUPPLIERS_SUCCESSFUL:
                    supplierList.dgSupplier.dataProvider = note.getBody() as XMLListCollection;
                    break;
				case CommonCommand.LOAD_SUPPLIERS_FAILED:
					supplierList.dgSupplier.errorString = "Failed to load suppliers";
					break;
				case CommonCommand.SUPPLIER_REMOVE_FAILED:
					alert.error(note.getBody() as String);
					break;
				case CommonCommand.SUPPLIER_ADDED:
				case CommonCommand.SUPPLIER_UPDATED:
				case CommonCommand.SUPPLIER_REMOVED:
					supplierProxy.search();
					break;
            }
        }
                    
        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return EmployeeList the viewComponent cast to EmployeeList
         */
        protected function get supplierList():SuppliersList
		{
            return viewComponent as SuppliersList;
        }
		
//		private function logout( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.APP_LOGOUT );
//		}
//		
//		private function addEmployee( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.ADD_EMPLOYEE );
//		}
//		
//		private function updateEmployee( event:Event = null ):void
//		{
//			sendNotification( ApplicationFacade.UPDATE_EMPLOYEE, supplierList.employees_li.selectedItem);
//		}
		
		private var supplierProxy:SupplierProxy;
		private var masterdataProxy:MasterdataProxy;
    }
}